package com.example.blog.controller;

import com.example.blog.pojo.Type;
import com.example.blog.pojo.query.SearchBlog;
import com.example.blog.service.BlogService;
import com.example.blog.service.TypeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * name: 东北宋小宝
 * date: 2020/9/1
 */
@Controller
public class TypeShowController {
    @Resource
    private TypeService typeService;
    @Resource
    private BlogService blogService;
    @Value("${blogpage.type}")
    private Integer page;

    /***
     * type分类查询页面
     * @param pageNum
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/types/{id}")
    public String type(@RequestParam(defaultValue = "1", value = "pageNum")Integer pageNum,
                       @PathVariable Long id , Model model){
        List<Type> types = typeService.findAll();
        id = (id<1) ? 1:id;
        PageInfo pageInfo = blogService.queryPage(pageNum, page, new SearchBlog(null, id,null));
        model.addAttribute("types",types);
        model.addAttribute("activeId",id);
        model.addAttribute("pageInfo",pageInfo);
        return "types";
    }
}
